﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace DigDes.DSchool.SUPS.Web.UI
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            
        }
        
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (GridView1.SelectedIndex == -1)
            {
                Edit_Button.Visible = false;
                Delete_Button.Visible = false;
                Cancel_Button.Visible = false;
                Edit_Button.Visible = false;
                DetailsView1.Visible = false;
                Save_Button.Visible = false;
            }
            else
            {
                Edit_Button.Visible = true;
                Delete_Button.Visible = true;
                Cancel_Button.Visible = true;
                Edit_Button.Visible = true;
                DetailsView1.Visible = true;
                Save_Button.Visible = true;
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            }
            
        }

        protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {

        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            FileUpload fu1 = (FileUpload)DetailsView1.FindControl("FileUpload1");
            Stream fs;
            BinaryReader br;
            Byte[] bytes=null;
            if (fu1!=null && fu1.HasFile)
            {
                try 
                {
                    if (Path.GetExtension(fu1.FileName).Equals(".jpeg") || Path.GetExtension(fu1.FileName).Equals(".jpg"))
                    {
                        
                        fs = fu1.PostedFile.InputStream;
                        br = new BinaryReader(fs);
                        bytes = br.ReadBytes((Int32)fs.Length);
                    }
                }
                catch (Exception ex)
                {
                   
                }
            }
            e.Values["Data"] = bytes;
        }

        protected void DetailsView1_PreRender(object sender, EventArgs e)
        {
            GridView1.DataBind();
            
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
            }
            if ( GridView1.SelectedIndex == -1 && DetailsView1.CurrentMode!=DetailsViewMode.Insert)
            {
                Edit_Button.Visible = false;
                Delete_Button.Visible = false;
                Cancel_Button.Visible = false;
                
                Save_Button.Visible = false;
                DetailsView1.Visible = false;
                infLabel.Visible = false;
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            }
            else if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                
                Cancel_Button.Visible = true;
                
                DetailsView1.Visible = true;
                Save_Button.Visible = true;
            }
            else
            {
                Edit_Button.Visible = true;
                Delete_Button.Visible = true;
                Cancel_Button.Visible = true;
                
                DetailsView1.Visible = true;
                Save_Button.Visible = true;
            }
            
        }

        protected void btAdd_Click(object sender, EventArgs e)
        {
            infLabel.Visible = true;
            infLabel.Text = "Добавление груза" ;
            DetailsView1.Visible = true;
            DetailsView1.ChangeMode(DetailsViewMode.Insert);
              
            Cancel_Button.Visible = true;
            Save_Button.Visible = true;

        }

        protected void btEdit_Click(object sender, EventArgs e)
        {
            infLabel.Visible = true;
            infLabel.Text = "Изменение " + GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            DetailsView1.DataBind();
            Cancel_Button.Visible = true;
            Save_Button.Visible = true;
                               
            
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            
            DetailsView1.DeleteItem();
            GridView1.SelectedIndex = 0;

        }

        protected void Save_Button_Click(object sender, EventArgs e)
        {
           
            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                ObjectDataSource1.InsertParameters.Add("Data", System.Data.DbType.Binary,null);    
                
                DetailsView1.InsertItem(true);
            }
            else
            {
                
                ObjectDataSource1.UpdateParameters.Add("Data", System.Data.DbType.Binary, null);
                DetailsView1.UpdateItem(true);
                
               
            }
            GridView1.SelectedIndex = -1;
            Cancel_Button.Visible = false;
            Save_Button.Visible = false;
        }

        protected void Cancel_Button_Click(object sender, EventArgs e)
        {
            GridView1.SelectedIndex = -1;
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
          
        }

        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            FileUpload fu1 = (FileUpload)DetailsView1.FindControl("FileUpload1");
            Stream fs;
            BinaryReader br;
            Byte[] bytes = null;
            if (fu1 != null && fu1.HasFile)
            {
                try
                {
                    if (Path.GetExtension(fu1.FileName).Equals(".jpeg") || Path.GetExtension(fu1.FileName).Equals(".jpg"))
                    {

                        fs = fu1.PostedFile.InputStream;
                        br = new BinaryReader(fs);
                        bytes = br.ReadBytes((Int32)fs.Length);
                    }
                }
                catch (Exception ex)
                {
                    
                }
            }
            
            e.NewValues["Data"] = bytes;
        }
    }
}